#!/bin/sh
#
# Load several components for debconf configuration

set -e

. /usr/share/debconf/confmodule

PDNSCONF=/etc/powerdns/pdns.conf

db_version 2.0
if [ ! -f $PDNSCONF ]; then
  db_input medium pdns-server/localaddress || true
  db_input medium pdns-server/allowrecursion || true

  db_go || true
else
  LOCAL=`(cat $PDNSCONF | grep "^local-address=" | awk -F '=' '{print $2}') || true`
  RECURSION=`(cat $PDNSCONF | grep "^allow-recursion=" | awk -F '=' '{print $2}') || true`

  # Put multiple lines on one line and separate them by a comma
  REC=""
  for i in $RECURSION; do
    REC="$i,$REC"
  done

  # Remove , on the end of the line
  RECURSION=`echo $REC | sed -e 's/,$//'`

  if [ ! -z "$RECURSION" ]; then
    db_set pdns-server/allowrecursion "$RECURSION"
  else
    db_set pdns-server/allowrecursion "127.0.0.1"
  fi
  if [ ! -z "$LOCAL" ]; then
    db_set pdns-server/localaddress $LOCAL
  else
    db_set pdns-server/localaddress "0.0.0.0"
  fi

  db_fset pdns-server/allowrecursion seen true
  db_fset pdns-server/localaddress seen true
fi

db_stop || true

exit 0

